package com.blb.thread;

public class Demo6DaemonThread {

    public static void main(String[] args) {
        //创建守护线程
        Thread daemon = new Thread(() -> {
            for (;;) {
                System.out.println("我在守护你们。。。。");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //设置守护线程
        daemon.setDaemon(true);
        daemon.start();
        //其它线程死亡后，守护线程一起死亡
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (int j = 0; j < 10; j++) {
                    System.out.println(Thread.currentThread().getName() + "--->" + j);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
